package com.cy.study.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.study.entity.CommentEntity;
import com.cy.study.mapper.CommentMapper;
import com.cy.study.req.AddCommentReq;
import com.cy.study.req.PageCommentReq;
import com.cy.study.res.PageCommentRes;
import com.cy.study.service.CommentService;
import com.cy.study.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentEntity> implements CommentService {

    @Override
    public Integer addComment(AddCommentReq req) {
        Integer userId = SecurityUtils.getUserId();
        CommentEntity comment = new CommentEntity();
        comment.setUserId(userId);
        comment.setTargetId(req.getTargetId());
        comment.setExperienceId(req.getExperienceId());
        comment.setBody(req.getBody());
        comment.setCreateTime(LocalDateTime.now());
        this.saveOrUpdate(comment);
        return comment.getId();
    }

    @Override
    public Page<PageCommentRes> pageComment(PageCommentReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<PageCommentRes> info = this.getBaseMapper().pageComment(req,page);
        return info;
    }
}
